package net.adriana.component;

import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import net.adriana.Fac;
import net.adriana.esaytool.Logger;
import net.adriana.ui.*;

public class MyFrame extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected MyPanel content;
	protected MyButton previous;
	protected MyButton exit;
	
	public MyFrame(int width,int height){
		ImageIcon ico = new ImageIcon("tool.png");
		setIconImage(ico.getImage());
		setTitle("实体类简易编写工具");
		setSize(width, height);
		setLocation(width,height);
		setResizable(false);
		content = new MyPanel();
		add(content);
		setFont(new Font(Font.SANS_SERIF,Font.BOLD,12));
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			Logger.log(Logger.ERROR, "界面皮肤获取失败 " + e.getMessage());
		}
	}
	
	
	public void setLocation(int x,int y){
		int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		super.setLocation(width/2-x/2,height/2-y/2);
	}
	
	public void addPreviousBtn(){
		if(Fac.currentWindow == Fac.first  && Fac.first != null){
			previous= new MyButton("重新登录");
		}else previous= new MyButton("上一步");
		if(Fac.currentWindow == Fac.last && Fac.last != null){
			previous.setBounds(335,270,55,20);
		}else previous.setBounds(270,270,55,20);
		previous.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(Fac.currentWindow == Fac.first  && Fac.first != null){
					Fac.currentWindow = new LoginWindow();
					dispose();
				}else if(Fac.currentWindow == Fac.second && Fac.second != null){
					if(Fac.first == null){
						Fac.first = new FirstWindow();
						Fac.first.addPreviousBtn();
						Fac.first.addNextBtn();
						Fac.first.addExitBtn();
						Fac.currentWindow = Fac.first;
					}else Fac.first.setVisible(true);
					Fac.currentWindow = Fac.first;
					Fac.second.setVisible(false);
				}else if(Fac.currentWindow == Fac.last && Fac.last != null){
					if(Fac.second == null){
						Fac.second = new SecondWindow();
						Fac.currentWindow = Fac.second;
						Fac.second.addPreviousBtn();
						Fac.second.addNextBtn();
						Fac.second.addExitBtn();
					}else Fac.second.setVisible(true);
					Fac.currentWindow = Fac.second;
					Fac.last.setVisible(false);
				}
			}
		});
		content.add(previous);
	}
	
	public void addExitBtn(){	
		exit = new MyButton("退出");
		exit.setBounds(400,270,55,20);
		exit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(alert() == JOptionPane.YES_OPTION) System.exit(0);
			}
		});
		content.add(exit);
	}
	
	protected void initLabel(String msg,int x, int y, int width, int height){
		JLabel label = new JLabel(msg);
		label.setBounds(x, y, width, height);
		content.add(label);
	}
	
	protected int alert(){
		return JOptionPane.showConfirmDialog(
				null, 
				"请确认是否退出",
				"退出", 
				JOptionPane.YES_NO_OPTION);
	}
}
